package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.IUserService;
import com.example.util.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/getUser")
    public User getUser(){
        return userService.getById(1);
    }

    @PostMapping("/getAllUser")
    public List<User> getAllUser(){
        return userService.findUserAll();
    }

    @PostMapping("/findAllUser")
    public ResultInfo findAllUser(){
        return userService.getAllUser();
    }

    /**
     * 根据用户名查找
     * @param name
     * @return
     */
    @GetMapping("/selectByName")
    public User selectByName(String name){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据id或name查找
     * @return
     */
    @GetMapping("/selectUserList")
    public List<User> selectUserList(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",3).or().eq("name","test");
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 表示更新name为test的用户的name
     * @param name
     * @return
     */
    @GetMapping("/updateByName1")
    public Integer updateByName1(String name){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("name",name);
        updateWrapper.eq("name","test");
        return userMapper.update(new User(),updateWrapper);
    }

    /**
     * 功能同上
     * @param name
     * @return
     */
    @GetMapping("/updateByName2")
    public Integer updateByName2(String name){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name","test1");
        User user=new User();
        user.setName(name);
        return userMapper.update(user,updateWrapper);
    }
}
